package logic.operation;

import java.io.IOException;
import java.util.Arrays;

import javax.swing.JTable;

import org.apache.log4j.Logger;

//TODO tez wyprowadzić z klasy abstrajcyjnej operacji dla selekcji
public class InvertSelectionOperation implements IFileOperation {
	
	Logger logger = Logger.getLogger(InvertSelectionOperation.class.getName());
	
	JTable table;
	
	public InvertSelectionOperation() {
		
	}
	
	public InvertSelectionOperation(JTable table) {
		this.table = table;
	}
	
	public void setTable(JTable table) {
		this.table = table;
	}
	
	public void execute() throws IOException {
		logger.info("Inverting selection");
		int [] selectedRows = table.getSelectedRows();
		Arrays.sort(selectedRows);
		table.clearSelection();
		for (int i = 0; i < table.getRowCount(); i++) {
			if (Arrays.binarySearch(selectedRows, i) < 0) {
				table.getSelectionModel().addSelectionInterval(i, i);
			}
		}
	}
	
	@Override
	public void cancel() {
		
	}
	@Override
	
	public void run() {
		try {
			execute();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public String getName() {
		return "Invert";
	}

}
